package com.renyi.action;

import java.io.ObjectOutputStream.PutField;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.google.gson.reflect.TypeToken;
import com.renyi.http.HttpResult;
import com.renyi.http.ParamBean;
import com.renyi.model.DoctorAppInfo;
import com.renyi.model.MybatisService;
import com.renyi.util.JSONUtils;

/**
 * 
 * @author shs
 * 
 */
@Controller
public class PatientController extends BaseController {
	@Autowired
	private MybatisService mybatisService;

	// 获取区列表
	@RequestMapping("getAreaList")
	public void getAreaList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String areaName = paramMap.get("areaName") != null ? paramMap.get("areaName").toString() : "";

			// if ("".equals(uid)) {
			// result.setCode(-1); // 0：成功
			// result.setMsg("用戶ID参数为空");
			// this.ajaxResultJson(response, result);
			// return;
			// }

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("areaName", areaName);

			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getAreaListSQL", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 获取医院列表
	@RequestMapping("getHospitalList")
	public void getHospitalList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String areaID = paramMap.get("areaID") != null ? paramMap.get("areaID").toString() : "";

			// if ("".equals(uid)) {
			// result.setCode(-1); // 0：成功
			// result.setMsg("用戶ID参数为空");
			// this.ajaxResultJson(response, result);
			// return;
			// }

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("areaID", areaID);

			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getHospitalListSQL", pMap);
			Long count = this.mybatisService.findIbatisListCount("getHospitalListCountSQL", pMap);
			count = count != null ? count : 0;
			result.setCode(0); // 0：成功
			result.setData(data);
			result.setRecordCount(count.intValue());
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 获取医生列表
	@RequestMapping("getDoctorList")
	public void getDoctorList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String areaName = paramMap.get("areaName") != null ? paramMap.get("areaName").toString() : "";
			String areaID = paramMap.get("areaID") != null ? paramMap.get("areaID").toString() : "";
			String hospitalID = paramMap.get("hospitalID") != null ? paramMap.get("hospitalID").toString() : "";
			// 1-全科 2-风湿科
			String departmentID = paramMap.get("departmentID") != null ? paramMap.get("departmentID").toString() : "1";

			Integer page = (int) (paramMap.get("page") != null
					? Math.floor(Float.valueOf(paramMap.get("page").toString())) : 1);
			Integer pageSize = (int) (paramMap.get("pageSize") != null
					? Math.floor(Float.valueOf(paramMap.get("pageSize").toString())) : 10);

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用戶ID参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			Integer beginRow = (page - 1) * pageSize + 1;
			pMap.put("beginRow", beginRow);
			pMap.put("endRow", pageSize * page);
			pMap.put("uid", uid);
			pMap.put("areaName", areaName);
			pMap.put("areaID", areaID);
			pMap.put("hospitalID", hospitalID);
			pMap.put("departmentID", departmentID);

			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getDoctorListSQL", pMap);
			Long count = this.mybatisService.findIbatisListCount("getDoctorListCountSQL", pMap);
			count = count != null ? count : 0;
			result.setCode(0); // 0：成功
			result.setData(data);
			result.setRecordCount(count.intValue());
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 查找医生列表
	@RequestMapping("queryDoctorList")
	public void queryDoctorList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String keyword  = paramMap.get("keyword") != null ? paramMap.get("keyword").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用戶ID参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("keyword", keyword);

			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("queryDoctorListSQL", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 快速提问
	@RequestMapping("/saveQuestion")
	public void saveQuestion(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String questionDes = paramMap.get("questionDes").toString().trim();
			String departmentID = paramMap.get("departmentID").toString().trim();

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("questionDes", questionDes);
			pMap.put("departmentID", departmentID);

			int key = (Integer) this.mybatisService.insertIbatisObject("saveQuestionSQL", pMap);

			if (key > 0) {
				result.setCode(0);
				result.setMsg("");
				;// //错误消息
			} else {
				result.setCode(-1);
				result.setMsg("");
				;// //错误消息
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 获取推荐医生列表
	@RequestMapping("getRecommendDoctor")
	public void getRecommendDoctor(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用戶ID参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();

			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getRecommendDoctorSQL", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 获取医生详情
	@RequestMapping("getDoctorDetail")
	public void getDoctorDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String doctorID = paramMap.get("doctorID") != null ? paramMap.get("doctorID").toString() : "";

			if ("".equals(doctorID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("医生编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("doctorID", doctorID);
			List<HashMap<String, Object>> gList = new ArrayList<HashMap<String, Object>>();
			List<HashMap<String, Object>> list = this.mybatisService.findMybatisList("getDoctorDetailSQL", pMap);
			try {
				gList = this.mybatisService.findMybatisList("getDoctorDetailAppraiseSQL", pMap);
			} catch (Exception e) {
				// TODO: handle exception
			}

			HashMap<String, Object> hs = new HashMap<String, Object>();
			if (list.size() > 0) {
				hs = list.get(0);
			}
			hs.put("gList", gList);
			result.setCode(0); // 0：成功
			result.setData(hs);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-获取医生评价
	@RequestMapping("getDoctorAppraise")
	public void getDoctorAppraise(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String doctorID = paramMap.get("doctorID") != null ? paramMap.get("doctorID").toString() : "";

			if ("".equals(doctorID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("医生编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("doctorID", doctorID);
			int appCount, appGood, appCommon, appBad;
			int appResult;
			String jsondata = "";
			List<DoctorAppInfo> gList = new ArrayList<DoctorAppInfo>();
			List<DoctorAppInfo> mList = new ArrayList<DoctorAppInfo>();
			List<DoctorAppInfo> bList = new ArrayList<DoctorAppInfo>();
			try {
				List<HashMap<String, Object>> list = this.mybatisService.findMybatisList("getDoctorAppraiseSQL", pMap);
				jsondata = JSON.toJSON(list).toString();
			} catch (Exception e) {
				// TODO: handle exception
			}

			TypeToken<ArrayList<DoctorAppInfo>> token = new TypeToken<ArrayList<DoctorAppInfo>>() {
			};
			List<DoctorAppInfo> dList = new ArrayList<DoctorAppInfo>();
			dList = JSONUtils.fromJson(jsondata, token);
			for (int i = 0; i < dList.size(); i++) {
				DoctorAppInfo dInfo = dList.get(i);
				if (dInfo != null) {
					appResult = dInfo.getAppResult();
					switch (appResult) {
					case 1:
						gList.add(dInfo);
						break;
					case 2:
						mList.add(dInfo);
						break;
					case 3:
						bList.add(dInfo);
						break;
					default:
						break;
					}
				}
			}

			HashMap<String, Object> rMap = new HashMap<String, Object>();
			rMap.put("appCount", dList.size());
			rMap.put("appGood", gList.size());
			rMap.put("appCommon", mList.size());
			rMap.put("appBad", bList.size());
			rMap.put("gList", gList);
			rMap.put("mList", mList);
			rMap.put("bList", bList);
			result.setCode(0); // 0：成功
			result.setData(rMap);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

}
